package com.app.Tree;

import java.io.File;
import java.util.ArrayList;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.app.Controller.ReadWriteFileController;


import ReadWriteXML.DOMUltil;
import ReadWriteXML.ReadXML;
import ReadWriteXML.WriteXML;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.Log;

public class TextNode extends INode{
	ArrayList<INode> nodes = new ArrayList<INode>();
	
	public TextNode(){
		
	}
	public TextNode(long id, String title, String text, float x, float y, String user, long vs, int lb, long pID) {
		super(id, title, text, x, y, user, vs,lb,pID);
		
	}

	@Override
	public void add(INode node) {
		node.setDepth(this.depth + 1);
		nodes.add(node);
		this.autoArrangeTextNode();
		Log.d("Phu", "Đã add textnode với id= "+node.id);
	}

	@Override
	public void onDraw(Canvas canvas) {
		this.heigh = 70;
		this.width = 250;
		ArrayList<String> lines = Split(this.text, 30);
		ArrayList<String> linesTitle = Split(this.title, 30);
//		if (lines.size() >= 3)
//			heigh = 20 + 15 * lines.size();
		RectF rect = new RectF(x, y, x + width , y + heigh);
		int space = 1;
		RectF border = new RectF(x - space, y - space, x + width + space, y+ heigh + space);

		Paint p1 = new Paint();
		p1.setAntiAlias(true);
		p1.setStrokeWidth(3);
		
		//p1.setARGB(150, 254, 249, 246);
		p1.setColor(Color.rgb(254, 249, 246));
//			p1.setARGB(150, 79, 129, 189);
		canvas.drawRect(rect, p1);

		Rect bounds = new Rect();
		p1.setColor(Color.BLACK);
		p1.setStrokeWidth(2);
		String t = linesTitle.get(0);
		if(lines.size()>1)
			t+="...";
		canvas.drawText(t, x + 5, y + 13, p1);
		canvas.drawText("by ", x + 5, y + 25, p1);
		p1.setColor(Color.BLUE);
		canvas.drawText(this.user + ":"+this.version, x + 23, y + 25, p1);
		//p1.setColor(Color.BLACK);
		//canvas.drawText(, x + 90, y + 25, p1);
		p1.setColor(Color.BLUE);
		p1.setStrokeWidth(1);
		canvas.drawLine(x+3, y+30, x+width, y+30, p1);
		p1.setColor(Color.BLACK);
		String s=lines.get(0);
		if(lines.size()>1)
			s+="...";
		canvas.drawText(s, x + 20, y + 47 + 0 * 15, p1);

		
		Paint p = new Paint();
		p.setStrokeWidth(2);
		p.setStyle(Paint.Style.STROKE);
		p.setAntiAlias(true);
		
		p.setColor(Color.BLUE);
//		p.setColor(Color.rgb(156, 93, 138));
//		canvas.drawRoundRect(border, 10, 10, p);
		canvas.drawRect(border, p);
		for (INode node : nodes) {
			node.onDraw(canvas);
		}
		
	}
	
	//An
	public void autoArrangeTextNode() {
		//Log.d("An","sắp xếp: ");
		int size = this.nodes.size();
		//Log.d("An","node size: " + size);
		INode cl = null;
		for (int i = 0; i < size; i++) {
			int z = 0;
			if (i > 0)
				z = nodes.get(i - 1).getSumChildren()+ 1;
			//Log.d("An","z: " + z);
			 if(z==0)
				 z=1;
			if (i > 0) {
				//Log.d("An","i > 0");
				nodes.get(i).setX(this.x + 30);
				nodes.get(i).setY(nodes.get(i - 1).y + z * (this.heigh + 15));
			} else {
				//Log.d("An","i = 0");
				nodes.get(i).setX(this.x + 30);
				nodes.get(i).setY(this.y + this.heigh + 15);
			}
			nodes.get(i).autoArrangeTextNode();
		}
	}
	//end
	
	@Override
	public INode getById(long id) {
		INode rs = null;
		if (this.id == id){
			Log.d("Phu", "Tìm node: getById: id="+this.id);
			return this;
		}

		for (INode node : nodes) {
			rs = node.getById(id);
			if (rs != null) {
				break;
			}
		}
		return rs;
	}

	@Override
	public ArrayList<INode> getChildren() {
		return nodes;
	}

	public INode getCollision(float x, float y) {
		INode rs = null;
		if (this.isCollision(x, y,0))
			return this;
		for (INode node : nodes) {
			rs = node.getCollision(x, y);
			if (rs != null)
				return rs;
		}
		return null;
	}

	public void setPostion(float x, float y) {
		super.setPostion(x, y);
	}

	public void moveAll(float moveX, float moveY) {
		super.setPostion(this.x + moveX, this.y + moveY);
		for (INode node : nodes) {
			node.moveAll(moveX, moveY);// (node.x+moveX, node.y+moveY);
		}
	}
	
	@Override
	public int getSumChildren() {
		int rs=this.nodes.size();
		for(INode node:this.nodes){
			rs+=node.getSumChildren();
		}
		return rs;
	}

	@Override
	public int getNodeHeight() {
		int rs=0;
		if(this.nodes.size()>0)
			rs++;
		return 0;
	}
	
	@Override
	public Element toXmlText(Document doc) {
		Element e=doc.createElement("TreeNode");
		e.setAttribute("ID", this.id+"");
		e.setAttribute("title", this.title);
		e.setAttribute("text", this.text+"");
		e.setAttribute("x", this.x+"");
		e.setAttribute("y", this.y+"");
		e.setAttribute("user",this.user);
		e.setAttribute("version", this.version+"");
		e.setAttribute("label",this.label+"");
		e.setAttribute("parentID", this.parentID+"");
		for(INode node:this.nodes){
			e.appendChild(node.toXmlText(doc));
		}		
		return e;
	}
	public synchronized void writeXML(String path){
		Log.d("file","TextNode - Đã ghi node xuong file:");
		File f = new File(path);
		if(f.exists())
			f.delete();
		Document doc = null;
		doc = WriteXML.insertRoot(doc, path, "Root");
		doc.getFirstChild().appendChild(this.toXmlText(doc));
		DOMUltil.writeXmlToFile(path, doc);
		
	}
	
	public void writeDeletedXML(String path, long id, long vs) {
		File f = new File(path);
		if (f.exists())
			f.delete();
		Document doc = null;		
		doc = WriteXML.insertRoot(doc, path, "Delete");
		doc.getFirstChild().appendChild(this.toXmlDelete(doc, id, vs));
		DOMUltil.writeXmlToFile(path, doc);

	}
	
	@Override
	public Element toXmlDelete(Document doc, long id, long vs) {
		Element e = doc.createElement("DeletedNode");
		e.setAttribute("ID", id + "");
		e.setAttribute("version", vs + "");
		return e;
	}

	@Override
	public INode readXML(Element element) {
		this.id=Long.parseLong(element.getAttribute("ID"));
		this.title=element.getAttribute("title");
		this.text=element.getAttribute("text");
		this.x=Float.parseFloat(element.getAttribute("x"));
		this.y=Float.parseFloat(element.getAttribute("y"));
		this.user=element.getAttribute("user");
		this.version = Long.parseLong(element.getAttribute("version"));
		this.label = Integer.parseInt(element.getAttribute("label"));
		this.parentID = Long.parseLong(element.getAttribute("parentID"));
		NodeList nodeList=element.getChildNodes();
		for( int i=0;i<nodeList.getLength();i++){
			TextNode n=new TextNode();
			this.add(n.readXML((Element) nodeList.item(i)));
		}
		return this;
	}
	public boolean clearChildren(){
		for(INode node:this.nodes){
			node.clearChildren();
			this.remove(node);
		}
		return true;
	}
	public void readXml(String path) {
		Document doc = ReadXML.BuildXML(path);		
		this.readXML((Element) doc.getFirstChild().getFirstChild());
	}
	
	@Override
	public void remove(INode node) {

		for (INode i : nodes) {
			i.remove(node);
		}
		nodes.remove(node);
		this.autoArrangeTextNode();
	}
	
	@Override
	public void autoArrange() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void zoomIn() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public boolean haveOtherChildren(String myUserName) {
		boolean rs=false;
		if(this.user!=myUserName)
			return true;
		for(INode node:nodes){
			rs=node.haveOtherChildren(myUserName);
			if(rs==true)
				break;
		}
		return rs;
	}
	@Override
	public INode getExpandCollision(float x, float y) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public int countLeaf() {
		// TODO Auto-generated method stub
		return 0;
	}
	@Override
	public void expand(boolean expand) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void showChildren(boolean show) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public String GetStringNode(long vs) {
		// TODO Auto-generated method stub
		return null;
	}
	
	
}
